/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 2013-2014 sagyf Yang. The Four Group.
 */

/**
 * Created by yfyang on 4/12/14.
 */
define(['adminapp'], function ($app) {
    $app.navactive('users');

    $app.formvalidator({
        'authUsers.password': {
            required : true,
            minlength: 5
        },
        confirm_password    : {
            required : true,
            minlength: 5,
            equalTo  : "#authUsersPassword"
        }
    }, 'authUsersForm');

    var $brand_group = $('#brand_form_group');

    var $brand = $('#brand_field');
    function initRepast(val) {
        $.get(g.ctx + 'admin/brand/merchant', {'merchant': val}, function (rst) {
            if (rst.status == 'OK') {
                var dat = rst.data;
                $brand.empty();
                $.each(dat, function (idx, val) {
                    $brand.append('<option value="'+val.id+'">'+val.name+'</option>');
                });
                $brand.select2({allowClear: true});
            }
        })
    }

    var $authUsersRole = $('#authUsers_role');
    var rule = $authUsersRole.val();
     if(rule){
         var brand = $authUsersRole.attr('data-brand');
         if (rule == 'merchant-repast' || rule == 'merchant-ktv') {
             initRepast(rule);
             $brand_group.show();
             setTimeout(function(){

                 $brand.select2('val', brand);
             }, 500);
         }
     }

    $authUsersRole.on('select2-selecting', function (e) {
        var val = e.val;
        if (val == 'merchant-repast' || val == 'merchant-ktv') {
            initRepast(val);
            $brand_group.show()
        } else {
            $brand_group.hide();
        }
    });
    return {};
});